<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="pyjm" label="" class="">
          <a-input v-model:value="formState.zjm" class="w-80" placeholder="姓名/助记码/病案号/住院号/床位号/结算ID" />
        </a-form-item>
        <a-form-item name="dateRange" label="出院日期" class="w-80 ml-2">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="radio" class="ml-8">
          <a-radio-group v-model:value="formState.status">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">未上报</a-radio>
            <a-radio value="1">已上报</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="radio" class="ml-8">
          <a-radio-group v-model:value="formState.napl">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">异地</a-radio>
            <a-radio value="1">本地</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="radio" class="ml-8">
          <a-radio-group v-model:value="formState.miFlag">
            <a-radio value="">全部</a-radio>
            <a-radio value="0">自费</a-radio>
            <a-radio value="2">医保</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start mb-2">
        <a-button type="primary" ghost class="mr-2" @click="onClickBatchReport">批量数据上报</a-button>
        <BaseExport type="病案上报" name="结算清单上报" code="000363" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 2800 }" column-code="000363" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'bazt'">
            {{ record.baztStr }}
          </template>
          <template v-if="column.dataIndex === 'miFlag'">
            {{ record.miFlag === 0 ? '自费' : '国家医保' }}
          </template>
          <!-- <template v-if="column.dataIndex === 'jhzt'">
            <a-tag v-if="record.jhzt === 0" color="green">待确认</a-tag>
            <a-tag v-if="record.jhzt === 1" color="blue">已确认</a-tag>
            <a-tag v-if="record.jhzt === 2" color="red">已作废</a-tag>
          </template> -->
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <!--bazt 0,'未上报',1,'已上报' , -->
              <a-button type="link" :disabled="record.sfbdby === 0 || record.miFlag === 0" size="small" @click="onPrint(record)">预览</a-button>
              <a-button v-if="record.bazt === 0" :disabled="record.sfbdby === 0 || record.miFlag === 0" type="link" size="small" @click="onClickReport(record)"
                >数据上报</a-button
              >
              <BaseButton
                v-if="record.bazt === 1"
                :disabled="record.sfbdby === 0 || record.miFlag === 0"
                confirm-text="确认撤销上报？"
                danger
                size="small"
                type="link"
                @click="cancelClickReport(record)"
                >撤销上报</BaseButton
              >
              <!-- <a-button v-if="record.bazt === 1" danger type="link" size="small" @click="cancelClickReport(record)">撤销上报</a-button> -->
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().add(-1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  zjm: '',
  status: '',
  napl: '1',
  miFlag: '2'
})

const { loading, pagination, tableData, getData } = useTable({
  url: '/med/MedicalRecord/getMedicalRecordReportpageList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      cystdate: params.dateRange[0],
      cyeddate: params.dateRange[1]
    }
  }
})
const cancelClickReport = async (row) => {
  const { code, msg } = await post('/med/MedicalRecord/doRevokeUpload', { inplsh: row.inplsh })
  if (code !== 0) return message.error(msg || '撤销上报失败')
  message.success('撤销上报成功')
  getData()
}
const fetchReport = async (params) => {
  const { code, msg } = await post('/med/MedicalRecord/medicalRecordReport', params)
  if (code !== 0) return message.error(msg || '数据上报失败')
  message.success('数据上报成功')
  getData()
}
const onPrint = async (record) => {
  const { code, msg, data } = await post('/med/MedicalRecord/printMedicalRecordsSettlementList', { inplsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
// 单条数据上报
const onClickReport = (row) => {
  const params = [
    {
      inplsh: row.inplsh
    }
  ]
  fetchReport(params)
}
// 批量数据上报
const onClickBatchReport = () => {
  const params = tableData.value.map((item) => ({
    inplsh: item.inplsh
  }))
  fetchReport(params)
}
</script>
